<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/webpage/include/taglib.jsp"%>
<html>
<head>
	<title>调度单管理</title>
	<meta name="decorator" content="ani"/>
	<script type="text/javascript">
		var validateForm;
		var $table; // 父页面table表格id
		var $topIndex;//弹出窗口的 index
		function doSubmit(table, index){//回调函数，在编辑和保存动作时，供openDialog调用提交表单。
		  if(validateForm.form()){
			  $table = table;
			  $topIndex = index;
			  jp.loading();
			  $("#inputForm").submit();
			  return true;
		  }

		  return false;
		}

		$(document).ready(function() {
			validateForm = $("#inputForm").validate({
				submitHandler: function(form){
					jp.post("${ctx}/tms/dispatch/save",$('#inputForm').serialize(),function(data){
						if(data.success){
	                    	$table.bootstrapTable('refresh');
	                    	jp.success(data.msg);
	                    	jp.close($topIndex);//关闭dialog

	                    }else{
            	  			jp.error(data.msg);
	                    }
					})
				},
				errorContainer: "#messageBox",
				errorPlacement: function(error, element) {
					$("#messageBox").text("输入有误，请先更正。");
					if (element.is(":checkbox")||element.is(":radio")||element.parent().is(".input-append")){
						error.appendTo(element.parent().parent());
					} else {
						error.insertAfter(element);
					}
				}
			});
			
	        $('#dispatchDDate').datetimepicker({
				 format: "YYYY-MM-DD HH:mm:ss"
		    });
	        $('#fetchDate').datetimepicker({
				 format: "YYYY-MM-DD HH:mm:ss"
		    });
		});
		
		function addRow(list, idx, tpl, row){
			$(list).append(Mustache.render(tpl, {
				idx: idx, delBtn: true, row: row
			}));
			$(list+idx).find("select").each(function(){
				$(this).val($(this).attr("data-value"));
			});
			$(list+idx).find("input[type='checkbox'], input[type='radio']").each(function(){
				var ss = $(this).attr("data-value").split(',');
				for (var i=0; i<ss.length; i++){
					if($(this).val() == ss[i]){
						$(this).attr("checked","checked");
					}
				}
			});
			$(list+idx).find(".form_datetime").each(function(){
				 $(this).datetimepicker({
					 format: "YYYY-MM-DD HH:mm:ss"
			    });
			});
		}
		function delRow(obj, prefix){
			var id = $(prefix+"_id");
			var delFlag = $(prefix+"_delFlag");
			if (id.val() == ""){
				$(obj).parent().parent().remove();
			}else if(delFlag.val() == "0"){
				delFlag.val("1");
				$(obj).html("&divide;").attr("title", "撤销删除");
				$(obj).parent().parent().addClass("error");
			}else if(delFlag.val() == "1"){
				delFlag.val("0");
				$(obj).html("&times;").attr("title", "删除");
				$(obj).parent().parent().removeClass("error");
			}
		}
        //获得所有选中的复选框的id
        function getCheckBox(){
            var boxs = "";		//设置变量，用于接收选中的复选框
            $("input:checkbox").each(function() {
                //判断复选框是否被选中
                if ($(this).is(':checked') && $(this).attr("id") != "allCheckBox") {
                    boxs += $(this).val();
                    boxs += ",";	//复选框的val()id集合以逗号分割
                }
            });
            return boxs;
        }
        <%--引入明细--%>
        function model(){
           /* alert();*/
            //获取调度单主表的启运区域、目的区域。启运城市和目的城市
            //var startArea = $("#startArea").val().trim();
            //var endArea = $("#endArea").val().trim();

            $.ajax({
                url : "${ctx}/tms/torder/findInfo",
                /*data : {		 //参数值
                    "startCity":startCity,
                    "endCity":endCity
                },*/
                type:"post",
                success : function(data) {
                    //console.log(data);
                    if(data){
                        var list = eval(data);
                        $("#myModal").modal("show");
                        $("#orderInfo").empty();
                        $.each(list, function (index, term) {
                            var addTr = $("<tr id='" + term.id + "'></tr>");
                            addTr.appendTo("#orderInfo");
                            var td = ("<td>" + "<input id='" + term.id + "' value='" + term.id + "' class='checkbox_css' type='checkbox' class='i-checks'>" + "</td>");
                            addTr.append(td);
                            addTr.append($('<td></td>').text(term.code));
                            addTr.append($('<td></td>').text(term.pickDate));
                            addTr.append($('<td></td>').text(term.pickAddress));
                            addTr.append($('<td></td>').text(term.goodsContacts));
                            addTr.append($('<td></td>').text(term.orderDate));
                            addTr.append($('<td></td>').text(term.truckType));
                            addTr.append($('<td></td>').text(term.totalWeight));
                            addTr.append($('<td></td>').text(term.totalVolume));
                            addTr.append($('<td></td>').text(term.remarks));
                        })
                    }else{
                        layer.msg("没有符合的订单信息！");
                    }
                },
                error : function(data){
                    alert("操作失败!");
                }
            });
        }
        <%--点击模态框提交后进行判断获取id--%>
        function detail() {
           /* alert();*/
            var tempIds = getCheckBox();
            //alert(temp);
            if (tempIds == null || tempIds == "") {
                layer.alert("请至少选择一条数据！");
                return;
            }else{
                $("#orderIds").val(tempIds);
                $('#myModal').modal('hide');
                $(".modal-backdrop").removeClass('in');
                $.ajax({
                    url : "${ctx}/tms/torder/findTmsOrderById",
                    data : {		 //参数值
                        "ids" :tempIds
                    },
                    type:"post",
                    success : function(data) {

                        if(data){
                             alert(data);
                            var list = eval(data);
                            var pickAddress=0;
                            var weight=0;
                            var volm=0;
                            var amount=0;
                            $.each(list, function (index,term) {
                                pickAddress+=term.pickAddress;

                                var addTr = $("<tr id='" + term.id + "'></tr>");
                                addTr.appendTo("#dispatchDetail");
                                addTr.append($('<td></td>').text(term.pickAddress));
                               /* addTr.append("<td>"
                                    + "&nbsp;<input onclick=\"deleteInPage('" + term.id + "')\"  type='button' value='删除' class='btn btn-danger btn-xs'>" + "</td>");
                           */ })


                        }
                    },
                    error : function(data){
                        alert("操作失败!");
                    }
                });
            }
        }
	</script>
</head>
<body class="bg-white">
		<form:form id="inputForm" modelAttribute="dispatch" action="${ctx}/tms/dispatch/save" method="post" class="form-horizontal">
		<form:hidden path="id"/>
		<sys:message content="${message}"/>
		<table class="table table-bordered">
		   <tbody>
				<tr>
					<td class="width-15 active"><label class="pull-right"><font color="red">*</font>调度单类型：</label></td>
					<td class="width-35">
						<form:select path="type" class="form-control required">
							<form:option value="" label=""/>
							<form:options items="${fns:getDictList('dispatch_type')}" itemLabel="label" itemValue="value" htmlEscape="false"/>
						</form:select>
					</td>
					<td class="width-15 active"><label class="pull-right"><font color="red">*</font>调度单编号：</label></td>
					<td class="width-35">
						<form:input path="code" htmlEscape="false" maxlength="64"    class="form-control required"/>
					</td>
				</tr>
				<tr>
					<td class="width-15 active"><label class="pull-right"><font color="red">*</font>调度时间：</label></td>
					<td class="width-35">
						<p class="input-group">
							<div class='input-group form_datetime' id='dispatchDDate'>
			                    <input type='text'  name="dispatchDDate" class="form-control required"  value="<fmt:formatDate value="${dispatch.dispatchDDate}" pattern="yyyy-MM-dd HH:mm:ss"/>"/>
			                    <span class="input-group-addon">
			                        <span class="glyphicon glyphicon-calendar"></span>
			                    </span>
			                </div>						            
			            </p>
					</td>
					<td class="width-15 active"><label class="pull-right">取货时间：</label></td>
					<td class="width-35">
						<p class="input-group">
							<div class='input-group form_datetime' id='fetchDate'>
			                    <input type='text'  name="fetchDate" class="form-control "  value="<fmt:formatDate value="${dispatch.fetchDate}" pattern="yyyy-MM-dd HH:mm:ss"/>"/>
			                    <span class="input-group-addon">
			                        <span class="glyphicon glyphicon-calendar"></span>
			                    </span>
			                </div>						            
			            </p>
					</td>
				</tr>
				<tr>
					<td class="width-15 active"><label class="pull-right">承运商：</label></td>
					<td class="width-35">
						<form:input path="carriers" htmlEscape="false" maxlength="64"    class="form-control "/>
					</td>
					<td class="width-15 active"><label class="pull-right">承运商电话：</label></td>
					<td class="width-35">
						<form:input path="carriersPhone" htmlEscape="false" maxlength="64"    class="form-control "/>
					</td>
				</tr>
				<tr>
					<td class="width-15 active"><label class="pull-right">承运单号：</label></td>
					<td class="width-35">
						<form:input path="carriersCode" htmlEscape="false" maxlength="64"    class="form-control "/>
					</td>
					<td class="width-15 active"><label class="pull-right">短运车辆号：</label></td>
					<td class="width-35">
						<form:input path="shortNumber" htmlEscape="false" maxlength="64"    class="form-control "/>
					</td>
				</tr>
				<tr>
					<td class="width-15 active"><label class="pull-right">车牌号：</label></td>
					<td class="width-35">
						<form:input path="truckNumber" htmlEscape="false" maxlength="64"    class="form-control "/>
					</td>
					<td class="width-15 active"><label class="pull-right">司机：</label></td>
					<td class="width-35">
						<form:input path="driver" htmlEscape="false" maxlength="64"    class="form-control "/>
					</td>
				</tr>
				<tr>
					<td class="width-15 active"><label class="pull-right">司机电话：</label></td>
					<td class="width-35">
						<form:input path="driverPhone" htmlEscape="false" maxlength="64"    class="form-control  isTel"/>
					</td>
					<td class="width-15 active"><label class="pull-right">承运商运费：</label></td>
					<td class="width-35">
						<form:input path="carriersCharge" htmlEscape="false" maxlength="64"    class="form-control "/>
					</td>
				</tr>
				<tr>
					<td class="width-15 active"><label class="pull-right">付款方式：</label></td>
					<td class="width-35">
						<form:input path="payMode" htmlEscape="false" maxlength="64"    class="form-control "/>
					</td>
					<td class="width-15 active"><label class="pull-right">短运费：</label></td>
					<td class="width-35">
						<form:input path="shortFreight" htmlEscape="false" maxlength="64"    class="form-control "/>
					</td>
				</tr>
				<tr>
					<td class="width-15 active"><label class="pull-right">付款方：</label></td>
					<td class="width-35">
						<form:input path="payer" htmlEscape="false" maxlength="64"    class="form-control "/>
					</td>
					<td class="width-15 active"><label class="pull-right">代收货款：</label></td>
					<td class="width-35">
						<form:input path="agencyPayment" htmlEscape="false" maxlength="64"    class="form-control "/>
					</td>
				</tr>
				<tr>
					<td class="width-15 active"><label class="pull-right">运费：</label></td>
					<td class="width-35">
						<form:input path="freight" htmlEscape="false" maxlength="64"    class="form-control "/>
					</td>
					<td class="width-15 active"><label class="pull-right">附加费：</label></td>
					<td class="width-35">
						<form:input path="totalAppendCharge" htmlEscape="false" maxlength="64"    class="form-control "/>
					</td>
				</tr>
				<tr>
					<td class="width-15 active"><label class="pull-right">总成本费用：</label></td>
					<td class="width-35">
						<form:input path="totalCostCharge" htmlEscape="false" maxlength="64"    class="form-control "/>
					</td>
					<td class="width-15 active"><label class="pull-right">现付：</label></td>
					<td class="width-35">
						<form:input path="payCash" htmlEscape="false" maxlength="64"    class="form-control "/>
					</td>
				</tr>
				<tr>
					<td class="width-15 active"><label class="pull-right">提付：</label></td>
					<td class="width-35">
						<form:input path="payExtract" htmlEscape="false" maxlength="64"    class="form-control "/>
					</td>
					<td class="width-15 active"><label class="pull-right">回付：</label></td>
					<td class="width-35">
						<form:input path="payRound" htmlEscape="false" maxlength="64"    class="form-control "/>
					</td>
				</tr>
				<tr>
					<td class="width-15 active"><label class="pull-right">车辆剩余空间重量：</label></td>
					<td class="width-35">
						<form:input path="truckSurplusWeight" htmlEscape="false" maxlength="64"    class="form-control "/>
					</td>
					<td class="width-15 active"><label class="pull-right">车辆剩余方量：</label></td>
					<td class="width-35">
						<form:input path="truckSurplusVolume" htmlEscape="false" maxlength="64"    class="form-control "/>
					</td>
				</tr>
				<tr>
					<td class="width-15 active"><label class="pull-right">总重量：</label></td>
					<td class="width-35">
						<form:input path="totalWeight" htmlEscape="false" maxlength="64"    class="form-control "/>
					</td>
					<td class="width-15 active"><label class="pull-right">总方数：</label></td>
					<td class="width-35">
						<form:input path="totalVolume" htmlEscape="false" maxlength="64"    class="form-control "/>
					</td>
				</tr>
				<tr>
					<td class="width-15 active"><label class="pull-right">总运费：</label></td>
					<td class="width-35">
						<form:input path="totalCharge" htmlEscape="false" maxlength="64"    class="form-control "/>
					</td>
					<td class="width-15 active"><label class="pull-right">运费成本：</label></td>
					<td class="width-35">
						<form:input path="freightCost" htmlEscape="false" maxlength="64"    class="form-control "/>
					</td>
				</tr>
				<tr>
					<td class="width-15 active"><label class="pull-right">预留1：</label></td>
					<td class="width-35">
						<form:input path="stay1" htmlEscape="false" maxlength="64"    class="form-control "/>
					</td>
					<td class="width-15 active"><label class="pull-right">预留2：</label></td>
					<td class="width-35">
						<form:input path="stay2" htmlEscape="false" maxlength="64"    class="form-control "/>
					</td>
				</tr>
				<tr>
					<td class="width-15 active"><label class="pull-right">备注信息：</label></td>
					<td class="width-35">
						<form:textarea path="remarks" htmlEscape="false" rows="4" maxlength="255"    class="form-control "/>
					</td>
					<td class="width-15 active"></td>
		   			<td class="width-35" ></td>
		  		</tr>
		 	</tbody>
		</table>
		<div class="tabs-container">
            <ul class="nav nav-tabs">
				<li class="active"><a data-toggle="tab" href="#tab-2" aria-expanded="true">调度订单明细：</a>
				</li>
				<li class=""><a data-toggle="tab" href="#tab-1" aria-expanded="false">调度附加费用：</a>
                </li>

            </ul>
            <div class="tab-content">
				<div id="tab-1" class="tab-pane fade ">
			<a class="btn btn-white btn-sm" onclick="addRow('#dispatchAppendList', dispatchAppendRowIdx, dispatchAppendTpl);dispatchAppendRowIdx = dispatchAppendRowIdx + 1;" title="新增"><i class="fa fa-plus"></i> 新增</a>
			<table class="table table-striped table-bordered table-condensed">
				<thead>
					<tr>
						<th class="hide"></th>
						<th>费用描述</th>
						<th>费用金额</th>
						<th>类型</th>
						<th>备注信息</th>
						<th width="10">&nbsp;</th>
					</tr>
				</thead>
				<tbody id="dispatchAppendList">
				</tbody>
			</table>
			<script type="text/template" id="dispatchAppendTpl">//<!--
				<tr id="dispatchAppendList{{idx}}">
					<td class="hide">
						<input id="dispatchAppendList{{idx}}_id" name="dispatchAppendList[{{idx}}].id" type="hidden" value="{{row.id}}"/>
						<input id="dispatchAppendList{{idx}}_delFlag" name="dispatchAppendList[{{idx}}].delFlag" type="hidden" value="0"/>
					</td>

					<td>
						<input id="dispatchAppendList{{idx}}_append" name="dispatchAppendList[{{idx}}].append" type="text" value="{{row.append}}" maxlength="64"  minlength="1"   class="form-control "/>
					</td>


					<td>
						<input id="dispatchAppendList{{idx}}_appendCharge" name="dispatchAppendList[{{idx}}].appendCharge" type="text" value="{{row.appendCharge}}"    class="form-control  isFloatGteZero"/>
					</td>


					<td>
						<input id="dispatchAppendList{{idx}}_appendType" name="dispatchAppendList[{{idx}}].appendType" type="text" value="{{row.appendType}}" maxlength="64"  minlength="1"   class="form-control "/>
					</td>


					<td>
						<textarea id="dispatchAppendList{{idx}}_remarks" name="dispatchAppendList[{{idx}}].remarks" rows="4" maxlength="255"  minlength="1"   class="form-control ">{{row.remarks}}</textarea>
					</td>

					<td class="text-center" width="10">
						{{#delBtn}}<span class="close" onclick="delRow(this, '#dispatchAppendList{{idx}}')" title="删除">&times;</span>{{/delBtn}}
					</td>
				</tr>//-->
			</script>
			<script type="text/javascript">
				var dispatchAppendRowIdx = 0, dispatchAppendTpl = $("#dispatchAppendTpl").html().replace(/(\/\/\<!\-\-)|(\/\/\-\->)/g,"");
				$(document).ready(function() {
					var data = ${fns:toJson(dispatch.dispatchAppendList)};
					for (var i=0; i<data.length; i++){
						addRow('#dispatchAppendList', dispatchAppendRowIdx, dispatchAppendTpl, data[i]);
						dispatchAppendRowIdx = dispatchAppendRowIdx + 1;
					}
				});
			</script>
			</div>
				<div id="tab-2" class="tab-pane fade in  active">
					<a href="#" onclick="model()"  class="btn btn-success btn-sm margin-left fl"> 引入明细</a>
<%--
			<a class="btn btn-white btn-sm" onclick="addRow('#dispatchDetailList', dispatchDetailRowIdx, dispatchDetailTpl);dispatchDetailRowIdx = dispatchDetailRowIdx + 1;" title="新增"><i class="fa fa-plus"></i> 新增</a>
--%>
			<table class="table table-striped table-bordered table-condensed">
				<thead>
					<tr>
						<th class="hide"></th>
						<th>订单编号</th>
						<th>提货时间</th>
						<th>提货地址</th>
						<th>托运人</th>
						<th>下单日期</th>
						<th>要求车型</th>
						<th>总重量（KG）</th>
						<th>总方数（m³）</th>
						<th>备注信息</th>
						<th width="10%">操作</th>
					</tr>
				</thead>
				<tbody id="dispatchDetail">
				<c:forEach items="${dispatch.dispatchDetailList}" var="item">
					<tr>
						<td>${item.orderCode}</td>
						<td>${item.pickAddress}</td>
						<td>${item.receiveAddress}</td>
						<td>${item.weight}</td>
						<td>${item.volume}</td>
						<td>${item.packaging}</td>
						<td>${item.remarks}</td>

						<%--<td>
							<c:if test="${flag eq 1}">
								<shiro:hasPermission name="tms:torder:view">
									<a href="#" onclick="openDialogView('查看订单明细', '${ctx}/tms/torder/form?id=${item.tmsOrder}&sign=1','800px', '500px')" class="btn btn-info btn-xs" ><i class="fa fa-search-plus"></i> 查看</a>
								</shiro:hasPermission>
							</c:if>
						</td>--%>
					</tr>
				</c:forEach>
				</tbody>
			</table>
			<%--<script type="text/template" id="dispatchDetailTpl">//<!--
				<tr id="dispatchDetailList{{idx}}">
					<td class="hide">
						<input id="dispatchDetailList{{idx}}_id" name="dispatchDetailList[{{idx}}].id" type="hidden" value="{{row.id}}"/>
						<input id="dispatchDetailList{{idx}}_delFlag" name="dispatchDetailList[{{idx}}].delFlag" type="hidden" value="0"/>
					</td>

					<td>
						<input id="dispatchDetailList{{idx}}_orderCode" name="dispatchDetailList[{{idx}}].orderCode" type="text" value="{{row.orderCode}}" maxlength="64"  minlength="1"   class="form-control required"/>
					</td>


					<td>
						<input id="dispatchDetailList{{idx}}_pickAddress" name="dispatchDetailList[{{idx}}].pickAddress" type="text" value="{{row.pickAddress}}" maxlength="64"  minlength="1"   class="form-control required"/>
					</td>


					<td>
						<input id="dispatchDetailList{{idx}}_receiveAddress" name="dispatchDetailList[{{idx}}].receiveAddress" type="text" value="{{row.receiveAddress}}" maxlength="64"  minlength="1"   class="form-control required"/>
					</td>


					<td>
						<input id="dispatchDetailList{{idx}}_packaging" name="dispatchDetailList[{{idx}}].packaging" type="text" value="{{row.packaging}}" maxlength="64"  minlength="1"   class="form-control required"/>
					</td>


					<td>
						<input id="dispatchDetailList{{idx}}_weight" name="dispatchDetailList[{{idx}}].weight" type="text" value="{{row.weight}}"    class="form-control required isFloatGtZero"/>
					</td>


					<td>
						<input id="dispatchDetailList{{idx}}_volume" name="dispatchDetailList[{{idx}}].volume" type="text" value="{{row.volume}}"    class="form-control required isFloatGtZero"/>
					</td>


					<td>
						<textarea id="dispatchDetailList{{idx}}_remarks" name="dispatchDetailList[{{idx}}].remarks" rows="4" maxlength="255"  minlength="1"   class="form-control ">{{row.remarks}}</textarea>
					</td>

					<td class="text-center" width="10">
						{{#delBtn}}<span class="close" onclick="delRow(this, '#dispatchDetailList{{idx}}')" title="删除">&times;</span>{{/delBtn}}
					</td>
				</tr>//-->
			</script>
			<script type="text/javascript">
				var dispatchDetailRowIdx = 0, dispatchDetailTpl = $("#dispatchDetailTpl").html().replace(/(\/\/\<!\-\-)|(\/\/\-\->)/g,"");
				$(document).ready(function() {
					var data = ${fns:toJson(dispatch.dispatchDetailList)};
					for (var i=0; i<data.length; i++){
						addRow('#dispatchDetailList', dispatchDetailRowIdx, dispatchDetailTpl, data[i]);
						dispatchDetailRowIdx = dispatchDetailRowIdx + 1;
					}
				});
			</script>--%>
			</div>
		</div>
		</div>
			<div class="modal fade" id="myModal" tabindex="-1" role="dialog"  aria-labelledby="myModalLabel" aria-hidden="true">
				<div class="modal-dialog">
					<div class="modal-content">
						<div class="modal-header">
							<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
							<h4 class="modal-title" id="myModalLabel">订单列表</h4>
						</div>
						<div class="modal-body modal-bodyNew">

							<!-- 表格 -->
							<div class="table-box1">
								<table id="content"
									   class="table table-striped table-bordered table-hover table-condensed dataTables-example dataTable  tableAjax">
									<thead>
									<tr>
										<th><input type="checkbox" id="allCheckBox" class="checkbox_css" name="all"></th>
										<th>订单编号</th>
										<th>提货时间</th>
										<th>提货地址</th>
										<th>托运人</th>
										<th>下单日期</th>
										<th>要求车型</th>
										<th>总重量（KG）</th>
										<th>总方数（m³）</th>
										<th>备注信息</th>
									</tr>
									</thead>
									<tbody id="orderInfo">

									</tbody>
								</table>
							</div>
						</div>
						<div class="modal-footer">

							<button type="button" class="btn btn-primary" onclick="detail()">确定</button>
							<button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
						</div>
					</div><!-- /.modal-content -->
				</div><!-- /.modal -->
			</div>
		</form:form>
</body>
</html>